// pages/recharge-center/recharge-center.js
var api = require('../../config/api.js');
var util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 0,

    totalCount: 0,
    totalOldPrice: 0,
    totalNewPrice: 0,
    strInputMoney: 0,
    defaultMoney: '',
    showBottom: 0,


  },
  //获取充值活动数据
  getData() {
    let that = this;
    util.request(api.RechargeActivityList).then(function(res) {
      if (res.errno == 0) {
        that.setData({
          list: res.data.recharges,
          bean: res.data
        })
      } else {
        util.myToast(res.errmsg)
      }
    });
  },
  //选中优惠卡
  onItemClick(e) {
    var index = e.currentTarget.dataset.index;
    var id = e.currentTarget.dataset.id;
    var tag = e.currentTarget.dataset.tag;

    var oldprice = e.currentTarget.dataset.oldprice;
    var newprice = e.currentTarget.dataset.newprice;
    var list = this.data.list;

    for (var x in list) {
      list[x].startNum = 0
    }
    if (list[index].id == id) {
      list[index].startNum = 1
    }

    this.setData({
      list: list,
      totalOldPrice: oldprice,
      totalNewPrice: newprice,
      id: id,
      totalCount: 1
    })
  },
  //优惠充值
  onRecharge: util.throttle(function(e) {
    var that = this;
    util.request(api.DiscountsRecharge, {
      id: that.data.id,
      num: 1
    }, 'post').then(function(res) {
      if (res.errno == 0) {
        that.onPay(res.data.orderSn);
      } else {
        util.myToast(res.errmsg)
      }
    });
  }, 1500),
  //自由充值
  onRechargeFree: util.throttle(function(e) {
    var that = this;
    util.request(api.FreedomRecharge, {
      amt: that.data.strInputMoney
    }, 'post').then(function(res) {
      if (res.errno == 0) {
        that.onPay(res.data.orderSn);
      } else {
        util.myToast(res.errmsg)
      }
    });
  },1500),
  //微信支付
  onPay: function(merReqNo) {
    let that = this;
    that.setData({
      showPaying: true
    })

    wx.login({
      success: res => {
        var code = res.code;

        util.request(api.StartPay, {
          merReqNo: merReqNo,
          code: code
        }, 'POST').then(function(res) {
          if (res.respCode == 'ZB0000') {
            var payParam = res;
            wx.requestPayment({
              'timeStamp': payParam.timestamp,
              'nonceStr': payParam.noncestr,
              'package': payParam.package,
              'signType': payParam.signtype,
              'paySign': payParam.paysign,
              'success': function(res) {
                //支付成功
                wx.switchTab({
                  url: '/pages/my-wallet/my-wallet',
                })
                wx.showToast({
                  title: '充值成功',
                })
                that.setData({
                  showPaying: false
                })

              },
              'fail': function(res) {
                //支付失败
                that.setData({
                  showPaying: false
                })
              },
            });
          } else {
            if (util.isEmpy(res.respDesc)) {
              util.myToast(res.errmsg);
            } else {
              util.myToast(res.respDesc);
            }
            that.setData({
              showPaying: false
            })
          }
        });

      }
    })

  },

  //步进器
  // onChangeStepper(event) {
  //   var num = event.detail;
  //   var index = event.target.dataset.index;

  //   var list = this.data.list;
  //   list[index].startNum = num;
  //   this.setData({
  //     list: list
  //   })
  //   this.calculateTotal();

  // },
  //tab
  onChangeTab(e) {
    var tab = e.detail.name;
    this.setData({
      showBottom: tab
    })
  },
  //获取输入金额
  getMoney(e) {
    var strmoney = e.detail.value;
    this.setData({
      strInputMoney: strmoney
    })
  },
  //清空输入金额
  onClearInput(e) {
    this.setData({
      defaultMoney: '',
      strInputMoney: 0
    })
  },
  /**
   * 计算商品总数
   */
  // calculateTotal: function() {
  //   var list = this.data.list;
  //   var totalCount = 0;
  //   var totalOldPrice = 0;
  //   var totalNewPrice = 0;
  //   for (var i = 0; i < list.length; i++) {
  //     var good = list[i];
  //     totalCount += good.startNum;
  //     totalOldPrice += good.startNum * good.faceVal;
  //     totalNewPrice += good.startNum * good.saleAmt;
  //   }
  //   totalOldPrice = totalOldPrice.toFixed(2);
  //   totalNewPrice = totalNewPrice.toFixed(2);
  //   this.setData({
  //     'totalCount': totalCount,
  //     'totalOldPrice': totalOldPrice,
  //     'totalNewPrice': totalNewPrice
  //   })

  // },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    this.getData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})